#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using System.Collections;
using Gizmox.WebGUI.Common.Resources;
using TVC.Layout;
using System.Xml;
using System.IO;

#endregion

namespace TVC.Modules.Ref
{
    public partial class Employee : ModuleParent, IHostedApplication
    {
        //int idAllowEditLanguage = 1;
        //int idAddNew = 1;
        //int idEdit = 1;
        //int idDelete = 1;
        //int idTransferIn = 1;
        //int idTransferOut = 1;
        //int idPrint = 1;
        GridLayout layout = null;

        public Employee()
        {
            InitializeComponent();
            _dgvList = dgvEmployee;
            _strType = "Employee";
        }

        public void InitializeApplication()
        {
        }

        //public void AddNew()
        //{

        //}

        public void SetObject(object obj)
        {
        }

        public HostedToolBarElement[] GetToolBarElements()
        {
            ArrayList objElements = new ArrayList();
            //if (IsPermis(idAddNew))
            //    objElements.Add(new HostedToolBarButton("Add New", new IconResourceHandle("Add.png"), "C"));
            //if (IsPermis(idEdit))
            //    objElements.Add(new HostedToolBarButton("Edit", new IconResourceHandle("Edit.png"), "A"));
            //if (IsPermis(idAddNew) && IsPermis(idEdit))
            //    objElements.Add(new HostedToolBarButton("Save", new IconResourceHandle("Save.png"), "S"));
            //if (IsPermis(idAddNew))
            //    objElements.Add(new HostedToolBarButton("Copy", new IconResourceHandle("Copy.png"), "O"));
            //if (IsPermis(idDelete))
            //    objElements.Add(new HostedToolBarButton("Delete", new IconResourceHandle("Delete.png"), "X"));
            //objElements.Add(new HostedToolBarSeperator());
            //if (IsPermis(idTransferIn))
            //    objElements.Add(new HostedToolBarButton("Transfer In", new IconResourceHandle("Down.png"), "TI"));
            //if (IsPermis(idTransferOut))
            //    objElements.Add(new HostedToolBarButton("Transfer Out", new IconResourceHandle("Up.png"), "TO"));
            //objElements.Add(new HostedToolBarSeperator());
            //if (IsPermis(idPrint))
            //    objElements.Add(new HostedToolBarButton("Print", new IconResourceHandle("Print.png"), "P"));
            objElements.Add(new HostedToolBarButton("To Excel", "To Excel", new IconResourceHandle("Excel.png"), "EXCEL"));
            objElements.Add(new HostedToolBarButton("Refresh", new IconResourceHandle("Refresh.png"), "R"));

            return (HostedToolBarElement[])objElements.ToArray(typeof(HostedToolBarElement));
        }

        public void OnToolBarButtonClick(HostedToolBarButton objButton, EventArgs objEvent)
        {
            try
            {
                sErr = "";
                string strAction = (string)objButton.Tag;


                switch (strAction)
                {
                    case "EXCEL":
                        export2Excel();
                        break;
                    case "R":

                        BUS.EmployeeControl ctr = new BUS.EmployeeControl();
                        BUS.SSRFANVControl ctrOrgan = new BUS.SSRFANVControl();
                        
                        DataTable dtExpC = ctrOrgan.GetAll_SSRFANVBU("T5", GetMainForm().SUNDB, ref sErr, MainForm.HLConnect);
                        ctr.BeginTransaction(ref sErr);
                        if (sErr == "") sErr = ctr.Delete();
                        foreach (DataRow r in dtExpC.Rows)
                        {
                            if (sErr != "") break;
                            DTO.EmployeeInfo inf = new DTO.EmployeeInfo()
                            {
                                EmployeeCode = r["CODE"].ToString(),
                                EmployeeName = r["NAME"].ToString()
                            };
                            if (sErr == "") ctr.Add_Employee(inf, ref sErr);
                        }

                        if (sErr != "")
                        {
                            ctr.RollbackTransaction(ref sErr);

                            MessageBox.Show(sErr, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, false);


                        }
                        else
                        {
                            ctr.CommitTransaction(ref sErr);
                            MessageBox.Show("IMPORT from Sunsytem successfully!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information, false);
                            GetMainForm().LoadDataTable(_strType);
                            Rebind();
                        }

                        break;
                }
                _processStatus = strAction;
                if (sErr == "") ThrowMsg("Command: " + _processStatus);
            }
            catch (Exception ex)
            {
                ThrowMsg(ex.Message + Environment.NewLine + ex.Source);
            }
        }

        private void Employee_Load(object sender, EventArgs e)
        {
            if (!GetMainForm().IsContainsTables(_strType)) GetMainForm().LoadDataTable(_strType);

            Rebind();
            layout = new GridLayout(MainForm.ServerPath + "Resources\\Layout\\", MainForm.ServerPath + "Documents\\Layout\\" + GetMainForm().PODInfo.USER_ID + "\\", _strType);
            layout.SetUpDGV(dgvEmployee);
            if (dgvEmployee.ShowFilterRow == true)
                mnuShowFilter.Checked = true;
        }
        private void Rebind()
        {
            bisEmployee.DataSource = GetMainForm().GetTable(_strType);
        }
    }
}